from common import *
import sys

default_data = ['','\\n', 10000]

def setup(data = default_data):
    import wx
    dlg = wx.TextEntryDialog(
        None, 'Enter value, that will cause the process to poison the input channel',
        'Configure Printer Process', str(data[0]))
    if dlg.ShowModal() == wx.ID_OK:
        try:
            data[0] = eval(dlg.GetValue())
        except:
            data[0] = dlg.GetValue()
    dlg.Destroy()

    dlg = wx.TextEntryDialog(
        None, 'Enter seperation string',
        'Configure Printer Process', str(data[1]))
    if dlg.ShowModal() == wx.ID_OK:
        data[1] = dlg.GetValue()
    dlg.Destroy()

    ok = False
    while not ok:
        dlg = wx.TextEntryDialog(
            None, 'Enter number of max reads. When exceeded, the channel is poisoned',
            'Configure Printer Process', str(data[2]))
        if dlg.ShowModal() == wx.ID_OK:
            try:
                data[2] = int(dlg.GetValue())
                ok = True
            except:
                pass
        else:
            ok = True
        dlg.Destroy()
    return data

def SYS_PrinterFunc(in0, data = default_data):
    i = 0
    while 1:
        if (i == data[2]):
            poison(in0)
            break
        t = in0()
        if (t == data[0]):
            poison(in0)
            break
        sys.stdout.write(str(t))

        try:
            sys.stdout.write(eval("'"+data[1]+"'"))
        except:
            sys.stdout.write(data[1])

        sys.stdout.flush()
        i = i + 1
 
